package com.hayden.fileMonitor.util;

import com.hayden.fileMonitor.pojo.tb_monitorlog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

public class JDBCUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    private static final Logger LOGGER = LoggerFactory.getLogger(JDBCUtils.class);
    static {
        ConfigFileReader reader = new ConfigFileReader();
        driver = reader.getProperty("db.driver");
        url = reader.getProperty("db.url");
        username = reader.getProperty("db.username");
        password = reader.getProperty("db.password");
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            //1、注册JDBC驱动
            Class.forName(driver);
            /* 2、获取数据库连接 */
            connection = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            LOGGER.error("驱动类加载失败，请检查数据库项配置：",e);
        } catch (SQLException e) {
            LOGGER.error("数据库连接失败，请检查数据库项配置：",e);
        }
        return connection;
    }
    /*关闭结果集、数据库操作对象、数据库连接*/
    public static void release(Connection connection, PreparedStatement pstm, ResultSet resultSet) {
        if(resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                LOGGER.error("资源释放失败：",e);
            }
        }
        if(pstm!=null) {
            try {
                pstm.close();
            } catch (SQLException e) {
                LOGGER.error("资源释放失败：",e);
            }
        }
        if(connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error("资源释放失败：",e);
            }
        }
    }

    private static void release(Connection connection, PreparedStatement pstm) {
        if(pstm!=null) {
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void insertOne(tb_monitorlog log) throws SQLException {
        String sql = "insert into tb_monitorlog (CreateTime,OperationUser,OperationObjectName,OperationType,OperationContent) values (?,?,?,?,?)";
        Connection connection = JDBCUtils.getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setTimestamp(1, (Timestamp) log.getCreateTime());
        pstm.setString(2,log.getOperationUser());
        pstm.setString(3,log.getOperationObjectName());
        pstm.setString(4,log.getOperationType());
        pstm.setString(5,log.getOperationContent());
        pstm.executeUpdate();
        release(connection,pstm);
    }


}
